<!-- 内容主体区域 -->
<link rel="stylesheet" href="layui/css/layui.css">
<script type="application/javascript" src="public/jquery.min.js"></script>
<div style="padding: 15px;">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">比赛图片</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-input-inline" style="">
                <div class="layui-input-inline">
                    <button id="add" class="layui-btn layui-btn-normal">新建比赛图片</button>
                </div>
            </div>
            <table class="layui-hide" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
            </script>

        </div>
    </div>


    <div class="layui-form">
        <table class="layui-table">
            <colgroup>
                <col width="150">
                <col width="150">
                <col width="150">
                <col width="200">
                <col width="150">
                <col width="150">
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>ID</th>
                <th>图片</th>
                <th>图片说明</th>
                <th>修改</th>
                <th>删除</th>

                <th>
                </th>
            </tr>
            </thead>
            <tbody id="l_data">

            </tbody>
        </table>
    </div>
</div>
<script src="layui/layui.js"></script>
<!--<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>-->
<script>
    //     url传参获取
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null)
            return unescape(r[2]);
        return null; //返回参数值
    }

    $(document).ready(function () {
//        var xx = getUrlParam('MatchID');
//        console.log(xx);
        $.ajax({
            type: "GET",
            url: "http://svcsa.org/ctfcpicture?matchid=" + getUrlParam('MatchID'),
            data: {},
            dataType: "json",
            async: false,
            headers: {
                Accept: "application/json" //设置header头
            },

            success: function (data) {
                var data = data.data;
                if (data.length > 0) {
                    var src = "";
                    for (var i = 0; i < data.length; i++) {
                        var id = data[i].ID;
                        var Src = data[i].Src
                        var Description = data[i].Description
                        src += "<tr>";
                        src += "<td>" + data[i].ID + "</td>";
                        src += "<td><img src='http://svcsa.org/uploads/" + data[i].Src + "'/></td>";
                        src += "<td><a>" + data[i].Description + "</a></td>";
                        src += "<td>" + '<button data-desc=' + data[i].Description + ' data-src="' + data[i].Src + '"  data-id="' + data[i].ID + '"   class="layui-btn layui-btn-normal layui-btn-sm edit"><i class="layui-icon"></i></button></td>';
                        src += "<td>" + '<button data-id="' + data[i].ID + '" class="layui-btn layui-btn-normal layui-btn-sm del"><i class="layui-icon-delete"></i></button></td>';
                        src += "</tr>";
                    }
                    $("#l_data").html(src);
                }


            }

        });

        function edit(id) {
            alert(id);
        }
    });

    layui.use(['table', 'form'], function () {

        var table = layui.table;
        var form = layui.form;

        $("#add").click(function () {
            layer.open({
                type: 2,
                title: '新建图片',
                shadeClose: true,
                shade: 0.8,
                area: ['40%', '50%'],
                content: 'ctfc_picture_add.html?MatchID=' + getUrlParam('MatchID'),
                success: function (layero, index) {

                },
                end: function () {
                    location.reload();
                }
            });
        })
        $(".edit").click(function () {
            var id = $(this).attr('data-id')
            var src = $(this).attr('data-src')
            var desc = $(this).attr('data-desc')
//            alert(id)
//             alert(src)
//              alert(desc)
            layer.open({
                type: 2,
                title: '修改图片',
                shadeClose: true,
                shade: 0.8,
                area: ['40%', '50%'],
                //                怎么获取到id
                content: 'ctfc_picture_edit.html?edit_id=' + id,
                success: function (layero, index) {
                    layer.iframeAuto(index);
                    var body = layer.getChildFrame('body', index);
                    body.find('#Src').attr("value", src)
                    body.find('#Description').attr("value", desc)
                    body.find('#demo1').attr("src", "http://svcsa.org/uploads/" + src)
                    body.find('#img_url').attr("value", src)
                },
                end: function () {
                    location.reload();
                }
            });
        })

        $(".del").click(function () {
            var id = $(this).attr('data-id')
//            alert(id)
            layer.confirm('确认删除', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                $.ajax({
                    url: 'http://svcsa.org/ctfcpicture/' + id,
                    type: 'delete',
                    dataType: 'json',
                    header: {
                        Accept: 'application/json'
                    },
                    success: function (data) {
                        console.log(data)
                    },
                    error: function (data) {
                        console.log(data)
                    }
                });
                layer.msg('删除成功', {
                    icon: 1,
                    time: 1200
                }, function () {
                    location.reload();
                });
            }, function () {
            });

        })


    });
</script>